# 异常的捕获(异常的处理)
"""
    基本语法：
        try:
            可能发生的错误代码
        exception:
            如果出现异常执行的代码
"""
# 基本捕获语法
# try:
#     f = open("D:\\file\\abc.txt", "r", encoding="utf-8")
# except:
#     print("出现异常了, 因为文件不存在, 将r模式改为w模式")
#     f = open("D:\\file\\abc.txt", "w", encoding="utf-8")
# 捕获指定异常
# try:
#     print(name)
#     # 1/0
# except NameError as e:
#     print("出现了未定义变量的异常")
#     print(e)

# 捕获多个异常
# try:
#     # 1 / 0
#     print(name)
# except (NameError, ZeroDivisionError) as e:
#     print("出现了变量未定义 或 除0异常")


# 捕获所有异常

try:
    f = open("D:\\file\\123.txt", "r", encoding="utf-8")
except Exception as e:
    print("出现异常了")
    f = open("D:\\file\\123.txt", "w", encoding="utf-8")
else:  # 没有异常执行的代码
    print("没有出现异常")
finally:  # 一定会执行的代码
    print("finally,有没有异常都要执行")
    f.close()
